/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.panes;

import com.eng.scc.core.parts.AbstractApplicationAction;
import com.eng.scc.core.parts.ActionCategory;
import com.eng.scc.core.parts.ApplicationViewAction;
import com.eng.scc.core.util.ImageFinder;
import com.eng.scc.core.view.ApplicationContext;
import com.eng.scc.core.view.ApplicationView;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Rafael
 */
public class ApplicationPane extends JPanel
{
    private ApplicationView selectedView = null;
    private List<ActionCategory> viewActions;

    public List<ActionCategory> getViewActions()
    {
        return viewActions;
    }
    
    public ApplicationPane()
    {
        initComponets();
    }
    
    private void initComponets()
    {
        setLayout( new GridBagLayout() );
        
        panelOptions.setLayout( new GridBagLayout() );
        panelOptions.add( appDrivers, 
                          new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, 
                                                  GridBagConstraints.EAST, 
                                                  GridBagConstraints.HORIZONTAL, 
                                                  new Insets( 5, 5, 5, 5 ), 0, 0 ) );

        panelOptions.add( appVehicle, 
                          new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, 
                                                  GridBagConstraints.EAST, 
                                                  GridBagConstraints.HORIZONTAL, 
                                                  new Insets( 5, 5, 5, 5 ), 0, 0 ) );

        panelOptions.add( appCompany, 
                          new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0, 
                                                  GridBagConstraints.EAST, 
                                                  GridBagConstraints.HORIZONTAL, 
                                                  new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        
        panelOptions.add( appFreight, 
                          new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0, 
                                                  GridBagConstraints.EAST, 
                                                  GridBagConstraints.HORIZONTAL, 
                                                  new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        
//        panelOptions.add( appSell, 
//                          new GridBagConstraints( 3, 0, 1, 1, 0.0, 0.0, 
//                                                  GridBagConstraints.EAST, 
//                                                  GridBagConstraints.HORIZONTAL, 
//                                                  new Insets( 5, 5, 5, 5 ), 0, 0 ) );
//
//        panelOptions.add( appOS, 
//                          new GridBagConstraints( 4, 0, 1, 1, 0.0, 0.0, 
//                                                  GridBagConstraints.EAST, 
//                                                  GridBagConstraints.HORIZONTAL, 
//                                                  new Insets( 5, 5, 5, 5 ), 0, 0 ) );
//
//        panelOptions.add( appFinancial, 
//                          new GridBagConstraints( 5, 0, 1, 1, 0.0, 0.0, 
//                                                  GridBagConstraints.EAST, 
//                                                  GridBagConstraints.HORIZONTAL, 
//                                                  new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        
        add( panelOptions, 
             new GridBagConstraints( 0, 0, 1, 1, 0.0, 1.0, 
                                    GridBagConstraints.NORTH, 
                                    GridBagConstraints.HORIZONTAL, 
                                    new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        
        tabbedPane.setFont( new Font( "Helvetica", Font.PLAIN, 14 ));
        tabbedPane.setTabPlacement( JTabbedPane.TOP );
        
        panelApplication.add( tabbedPane, 
                                  new GridBagConstraints( 0, 1, 1, 1, 1.0, 1.0, 
                                                            GridBagConstraints.CENTER, 
                                                            GridBagConstraints.BOTH, 
                                                            new Insets( 0, 0, 0, 0 ), 0, 0 ) );
//        panelApplication.setBackground( Color.red );
//        panelOptions.setBackground( Color.red );
        panelApplication.setBorder( BorderFactory.createEtchedBorder() );
        add( panelApplication, 
             new GridBagConstraints( 1, 0, 1, 1, 1.0, 1.0, 
                                    GridBagConstraints.CENTER, 
                                    GridBagConstraints.BOTH, 
                                    new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        
        panelApplication.add( panelButtons, 
                              new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, 
                                                      GridBagConstraints.WEST, 
                                                      GridBagConstraints.HORIZONTAL, 
                                                      new Insets( 0, 0, 5, 0 ), 0, 0 ) );
        
        group.add( appDrivers );
//        group.add( appFinancial );
        group.add( appCompany );
//        group.add( appOS );
        group.add( appVehicle );
        group.add( appFreight );
//        group.add( appUsers );
        
        appDrivers.setFont( new Font( "Helvetica", Font.PLAIN , 14 ));
        appDrivers.setMargin( new Insets( 5, 5, 5, 5) );
        appDrivers.setIcon( ImageFinder.getImageIcon( "tb_driver.png") );
        appDrivers.setBorderPainted( false );
        appDrivers.setOpaque( false );
        appDrivers.setFocusable(false);
        appDrivers.setContentAreaFilled(false);
        appDrivers.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ));
        
        appDrivers.setVerticalTextPosition(SwingConstants.BOTTOM); 
        appDrivers.setHorizontalTextPosition(SwingConstants.CENTER);
        
        appCompany.setFont( new Font( "Helvetica", Font.PLAIN , 14 ));
        appCompany.setMargin( new Insets( 5, 5, 5, 5) );
        appCompany.setIcon( ImageFinder.getImageIcon( "tb_factory.png") );
        appCompany.setOpaque( false );
        appCompany.setFocusable(false);
        appCompany.setContentAreaFilled(false);
        appCompany.setBorderPainted( false );
        appCompany.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ));
        
        
        appCompany.setVerticalTextPosition(SwingConstants.BOTTOM); 
        appCompany.setHorizontalTextPosition(SwingConstants.CENTER);
        
        appVehicle.setIcon( ImageFinder.getImageIcon( "tb_truck.png") );
        appVehicle.setFont( new Font( "Helvetica", Font.PLAIN , 14 ));
        appVehicle.setMargin( new Insets( 5, 5, 5, 5) );
        appVehicle.setOpaque( false );
        appVehicle.setFocusable(false);
        appVehicle.setContentAreaFilled(false);
        appVehicle.setBorderPainted( false );
        appVehicle.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ));
        
        appVehicle.setVerticalTextPosition(SwingConstants.BOTTOM); 
        appVehicle.setHorizontalTextPosition(SwingConstants.CENTER);
        
        appFreight.setIcon( ImageFinder.getImageIcon( "tb_box-post.png") );
        appFreight.setFont( new Font("Helvetica", Font.PLAIN, 14));
        appFreight.setMargin( new Insets( 5, 5, 5, 5) );
        appFreight.setOpaque( false );
        appFreight.setFocusable(false);
        appFreight.setContentAreaFilled(false);
        appFreight.setBorderPainted( false );
        
        appFreight.setVerticalTextPosition(SwingConstants.BOTTOM); 
        appFreight.setHorizontalTextPosition(SwingConstants.CENTER);

        appFreight.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ));
        
        appDrivers.addActionListener( listener );
        appCompany.addActionListener( listener );
        appFreight.addActionListener( listener );
//        appOS.addActionListener( listener );
        appVehicle.addActionListener( listener );
//        appSell.addActionListener( listener );
//        appUsers.addActionListener( listener );
        panelButtons.setLayout( new GridBagLayout() );
        tabbedPane.addChangeListener( new ChangeListener() {

            @Override
            public void stateChanged( ChangeEvent e )
            {
                if ( tabbedPane.getSelectedComponent() != null )
                {
                    selectedView = ( ApplicationView) tabbedPane.getSelectedComponent();
                    
                    if ( selectedView.getViewActions() != null )
                    {
                        createApplicationActions( selectedView.getViewActions() );
                        viewActions = selectedView.getViewActions();
                    }
                    
                    selectedView.refreshContent();
                    revalidate();
                    
                    fireEvents();
                }
            }
        } );
    }
    
    private void updateView( Object source )
    {
        try
        {
            boolean isVisible = false;
            
            for ( int i = 0; i < tabbedPane.getTabCount(); i++)
            {
                String c = tabbedPane.getTitleAt( i );
                ApplicationView view = (ApplicationView)tabbedPane.getComponentAt( i );

                if ( ((ApplicationViewAction) source).getRuntime().equals( view.getClass().getName() ) )
                {
                    selectedView = view;
                    
                    tabbedPane.setSelectedIndex( i );
                    isVisible = true;
                    
                    if ( view.getViewActions() != null )
                    {
                        createApplicationActions( view.getViewActions() );
                        viewActions = view.getViewActions();
                    }
                    view.refreshContent();
                    revalidate();
                    
                    fireEvents();
                    
                    break;
                }
            }
            
            if ( ! isVisible )
            {
                Class clazz = Class.forName( ((ApplicationViewAction) source).getRuntime() );

                ApplicationView view = (ApplicationView) clazz.newInstance();

                selectedView = view;
                
                if ( view.getViewActions() != null )
                {
                    createApplicationActions( view.getViewActions() );
                    viewActions = view.getViewActions();
                }

                view.refreshContent();

                tabbedPane.addTab( "<html><body leftmargin=5 topmargin=5 marginwidth=5 marginheight=5>"+((ApplicationViewAction) source).getText()+"</body></html>", view );
                tabbedPane.setSelectedComponent( view );
                
                revalidate();

                fireEvents();

                if ( view.getInitialFocus() != null )
                {
                    view.getInitialFocus().requestFocus();
                }
            }
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    private ActionListener listener = new ActionListener() 
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            updateView( e.getSource() );
        }
    };
    
    private JPanel panelOptions = new JPanel()
    {
        @Override
        public void paintComponent( Graphics g )
        {
            super.paintComponent( g );

            Graphics2D g2d = ( Graphics2D ) g.create();

            g2d.setPaint( new GradientPaint( 0, 0, new Color( 67, 205, 128), 500,
                                             100, Color.WHITE, true ) );

            Rectangle rec = new Rectangle( 0, 0, getWidth(), getHeight() );
            g2d.fill( rec );

            g2d.setPaint( new GradientPaint( 0, 0, Color.WHITE, 130,
                                             110, new Color( 51, 102, 255 ), false ) );

//            Ellipse2D.Double ellip = new Ellipse2D.Double( -150, -100, getWidth() / 2, getHeight() * 2 );
//            g2d.fill( ellip );
        }
    };
    
    private JTabbedPane tabbedPane = new JTabbedPane();
    
    private JPanel panelApplication = new JPanel( new GridBagLayout())
    {
        @Override
        public void paintComponent( Graphics g )
        {
            super.paintComponent( g );

            Graphics2D g2d = ( Graphics2D ) g.create();

            g2d.setPaint( new GradientPaint( 0, 0, new Color( 67, 205, 128), 500,
                                             100, Color.WHITE, true ) );

            Rectangle rec = new Rectangle( 0, 0, getWidth(), getHeight() );
            g2d.fill( rec );

            g2d.setPaint( new GradientPaint( 0, 0, Color.WHITE, 130,
                                             110, new Color( 51, 102, 255 ), false ) );

//            Ellipse2D.Double ellip = new Ellipse2D.Double( -150, -100, getWidth() / 2, getHeight() * 2 );
//            g2d.fill( ellip );
        }
    };
    
    private ButtonGroup group = new ButtonGroup();
    
    private ApplicationViewAction appDrivers = new ApplicationViewAction( "Motoristas", "com.eng.scc.core.view.DriverApplicationView" );
    private ApplicationViewAction appVehicle = new ApplicationViewAction( "Veículos", "com.eng.scc.core.view.VehicleApplicationView" );
    private ApplicationViewAction appCompany = new ApplicationViewAction( "Empresas", "com.eng.scc.core.view.CompanyApplicationView" );
    private ApplicationViewAction appFreight =   new ApplicationViewAction( "Viagens", "com.eng.scc.core.view.FreightApplicationView");
//    private ApplicationViewAction appSell = new ApplicationViewAction( "Venda", "com.eng.skynet.view.SellApplicationView");
//    private ApplicationViewAction appOS = new ApplicationViewAction( "OS", "com.eng.skynet.view.ServiceOrderApplicationView");
//    private ApplicationViewAction appFinancial = new ApplicationViewAction( "Movimentações", "com.eng.skynet.view.FinancialCashApplicationView");

    private JPanel panelButtons = new JPanel();
    
    private void createApplicationActions( List<ActionCategory> viewActions )
    {
        panelButtons.removeAll();
        
        int x = 0;
        
        for ( ActionCategory b : viewActions )
        {
            for ( AbstractApplicationAction action : b.getActions() )
            {
                JButton button = new JButton( ImageFinder.getImageIcon( action.getImg() ) );
                button.setAction( action );
                button.setToolTipText( action.getName() );
                button.setBorderPainted( false );
                button.setOpaque( false );
                button.setFocusable(false);
                button.setContentAreaFilled(false);
//                button.setMinimumSize( new Dimension( 25, 25 ) );
                button.setMargin( new java.awt.Insets( 1, 1, 1, 1 ) );
//                button.setPreferredSize( new Dimension( 80, 25 ) );
                
                panelButtons.add( button, 
                                  new GridBagConstraints( x++, 0, 1, 1, 0.0, 0.0, 
                                                          GridBagConstraints.WEST, 
                                                          GridBagConstraints.NONE, 
                                                          new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            }
        }
        
        panelButtons.add( new JPanel(), 
                          new GridBagConstraints( x, 0, 1, 1, 1.0, 0.0, 
                                                  GridBagConstraints.WEST, 
                                                  GridBagConstraints.HORIZONTAL, 
                                                  new Insets( 0, 0, 0, 0 ), 0, 0 ) );
    }
    
    @Override
    public void paintComponent( Graphics g )
    {
        super.paintComponent( g );

        Graphics2D g2d = ( Graphics2D ) g.create();

        g2d.setPaint( new GradientPaint( 0, 0, new Color( 67, 205, 128), 500,
                                         100, Color.WHITE, true ) );

        Rectangle rec = new Rectangle( 0, 0, getWidth(), getHeight() );
        g2d.fill( rec );

        g2d.setPaint( new GradientPaint( 0, 0, Color.WHITE, 130,
                                         110, new Color( 51, 102, 255 ), false ) );

//            Ellipse2D.Double ellip = new Ellipse2D.Double( -150, -100, getWidth() / 2, getHeight() * 2 );
//            g2d.fill( ellip );
    }

    private List<ChangeListener> changeListeners = new ArrayList<ChangeListener>();
    
    public void addEventListener( ChangeListener changeListener )
    {
        changeListeners.add( changeListener );
    }
    
    private void fireEvents()
    {
        for ( ChangeListener changeListener : changeListeners )
        {
            changeListener.stateChanged( null );
        }
    }
}
